mirror of
https://github.com/chenasraf/chrome-url-loader.git
synced 2026-05-17 17:48:02 +00:00
Added tests + fix
This commit is contained in:
10
.babelrc
Normal file
10
.babelrc
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"presets": [[
|
||||
"env",
|
||||
{
|
||||
"targets": {
|
||||
"node": "4"
|
||||
}
|
||||
}
|
||||
]]
|
||||
}
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -57,3 +57,4 @@ typings/
|
||||
# dotenv environment variables file
|
||||
.env
|
||||
|
||||
test/build
|
||||
|
||||
3
index.js
3
index.js
@@ -34,6 +34,7 @@ module.exports = function chromeUrlLoader(contents) {
|
||||
|
||||
options.baseDir = options.baseDir || process.cwd()
|
||||
options.baseDir = options.baseDir.endsWith('/') ? options.baseDir.slice(0, -1) : options.baseDir
|
||||
options.publicDir = options.publicDir || ''
|
||||
options.publicDir = options.publicDir.endsWith('/') ? options.publicDir.slice(0, -1) : options.publicDir
|
||||
|
||||
const relativeFilePath = this.resourcePath.replace(options.baseDir + '/', '')
|
||||
@@ -43,7 +44,7 @@ module.exports = function chromeUrlLoader(contents) {
|
||||
}
|
||||
|
||||
const fileName = path.basename(relativeFilePath)
|
||||
const outputDir = path.join(this.options.context, options.publicDir)
|
||||
const outputDir = path.join(this.context, options.publicDir)
|
||||
const outputPath = path.join(outputDir, fileName)
|
||||
const relativeDir = options.publicDir.split(path.sep).slice(1).join(path.sep)
|
||||
|
||||
|
||||
13
package.json
13
package.json
@@ -10,5 +10,18 @@
|
||||
"dependencies": {
|
||||
"loader-utils": "^1.1.0",
|
||||
"schema-utils": "^0.4.5"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "jest"
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel-jest": "^23.0.0-alpha.0",
|
||||
"babel-preset-env": "^1.6.1",
|
||||
"jest": "^23.0.0-alpha.0",
|
||||
"memory-fs": "^0.4.1",
|
||||
"webpack": "^4.1.1"
|
||||
},
|
||||
"jest": {
|
||||
"testEnvironment": "node"
|
||||
}
|
||||
}
|
||||
|
||||
35
test/compiler.js
Normal file
35
test/compiler.js
Normal file
@@ -0,0 +1,35 @@
|
||||
const path = require('path')
|
||||
const webpack = require('webpack')
|
||||
const memoryfs = require('memory-fs')
|
||||
|
||||
module.exports = (fixture, options = {}) => {
|
||||
const compiler = webpack({
|
||||
context: __dirname,
|
||||
entry: `./${fixture}`,
|
||||
output: {
|
||||
path: path.resolve(__dirname),
|
||||
filename: 'bundle.js',
|
||||
},
|
||||
module: {
|
||||
rules: [{
|
||||
test: /\.svg$/,
|
||||
use: {
|
||||
loader: path.resolve(__dirname, '../index.js'),
|
||||
options: {
|
||||
publicDir: 'build/static/svg'
|
||||
}
|
||||
}
|
||||
}]
|
||||
},
|
||||
})
|
||||
|
||||
compiler.outputFileSystem = new memoryfs()
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
compiler.run((err, stats) => {
|
||||
if (err) reject(err)
|
||||
|
||||
resolve(stats)
|
||||
})
|
||||
})
|
||||
}
|
||||
9
test/test.js
Normal file
9
test/test.js
Normal file
@@ -0,0 +1,9 @@
|
||||
const compiler = require('./compiler.js')
|
||||
|
||||
test('Inserts file path to output properly', (done) => {
|
||||
compiler('test.svg').then((stats) => {
|
||||
const output = stats.toJson().modules[0].source
|
||||
expect(output).toBe(`module.exports = chrome.extension.getURL('static/svg/test.svg')`)
|
||||
done()
|
||||
})
|
||||
})
|
||||
1
test/test.svg
Normal file
1
test/test.svg
Normal file
@@ -0,0 +1 @@
|
||||
<?xml version="1.0" standalone="no"?><!-- Generator: Gravit.io --><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="isolation:isolate" viewBox="0 0 12 4" width="12" height="4"><defs><clipPath id="_clipPath_hydSffmOoJOiUzgRtdngsyVWsB0fkp6C"><rect width="12" height="4"/></clipPath></defs><g clip-path="url(#_clipPath_hydSffmOoJOiUzgRtdngsyVWsB0fkp6C)"><g transform="matrix(1,0,0,1,0,0)"><text transform="matrix(1,0,0,1,0.129,3.207)" style="font-family:"Open Sans";font-weight:400;font-size:3px;font-style:normal;fill:#000000;stroke:none;">©casraf</text></g></g></svg>
|
||||
|
After Width: | Height: | Size: 617 B |
Reference in New Issue
Block a user