mirror of
https://github.com/chenasraf/chrome-url-loader.git
synced 2026-05-18 01:49:02 +00:00
First version
This commit is contained in:
25
README.md
25
README.md
@@ -1,2 +1,25 @@
|
||||
# chrome-url-loader
|
||||
Chrome Extension URL loader for webpack
|
||||
Chrome Extension URL loader for webpack.
|
||||
|
||||
This loader links to the file appropriately, using `chrome.extension.getURL` and also creates
|
||||
the file for you in the specified output dir.
|
||||
|
||||
### Usage
|
||||
|
||||
Use the loader as you would any other one, specify the test files and options:
|
||||
|
||||
```javascript
|
||||
{
|
||||
loader: require.resolve('chrome-url-loader'),
|
||||
test: /\.(png|svg|jpe?g|bmp|gif)/i,
|
||||
options: {
|
||||
publicDir: 'build/static/images',
|
||||
baseDir: paths.appSrc
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Options
|
||||
- baseDir - Your absolute source root directory (e.g. `/path/to/src`)
|
||||
- publicDir - The output dir to save the file to (e.g. `build/static/media`)
|
||||
|
||||
|
||||
56
index.js
Normal file
56
index.js
Normal file
@@ -0,0 +1,56 @@
|
||||
'use strict'
|
||||
|
||||
const loaderUtils = require('loader-utils')
|
||||
const validateOptions = require('schema-utils')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
|
||||
const schema = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
publicDir: {
|
||||
type: 'string'
|
||||
},
|
||||
baseDir: {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function mkDirP(dir) {
|
||||
const baseDir = '/'
|
||||
dir.split(path.sep).reduce((parent, child) => {
|
||||
const full = path.resolve(baseDir, parent, child)
|
||||
if (!fs.existsSync(full)) {
|
||||
fs.mkdirSync(full)
|
||||
}
|
||||
return full
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = function chromeUrlLoader(contents) {
|
||||
const options = loaderUtils.getOptions(this) || {}
|
||||
validateOptions(schema, options, 'Chrome URL Loader')
|
||||
|
||||
options.baseDir = options.baseDir || ''
|
||||
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 + '/', '')
|
||||
|
||||
if (relativeFilePath === this.resourcePath) {
|
||||
return `module.exports = '${relativeFilePath}'`
|
||||
}
|
||||
|
||||
const fileName = path.basename(relativeFilePath)
|
||||
const outputDir = path.join(this.options.context, options.publicDir)
|
||||
const outputPath = path.join(outputDir, fileName)
|
||||
const relativeDir = options.publicDir.split(path.sep).slice(1).join(path.sep)
|
||||
|
||||
mkDirP(outputDir)
|
||||
|
||||
fs.writeFileSync(outputPath, contents)
|
||||
|
||||
return `module.exports = chrome.extension.getURL('${relativeDir}/${fileName}')`
|
||||
}
|
||||
14
package.json
Normal file
14
package.json
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "chrome-url-loader",
|
||||
"version": "1.0.0",
|
||||
"description": "chrome extension URL loader for webpack",
|
||||
"main": "index.js",
|
||||
"repository": "https://github.com/chenasraf/chrome-url-loader.git",
|
||||
"author": "Chen Asraf <chen@asraf.me>",
|
||||
"license": "Apache-2.0",
|
||||
"private": false,
|
||||
"dependencies": {
|
||||
"loader-utils": "^1.1.0",
|
||||
"schema-utils": "^0.4.5"
|
||||
}
|
||||
}
|
||||
54
yarn.lock
Normal file
54
yarn.lock
Normal file
@@ -0,0 +1,54 @@
|
||||
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
|
||||
# yarn lockfile v1
|
||||
|
||||
|
||||
ajv-keywords@^3.1.0:
|
||||
version "3.1.0"
|
||||
resolved "https://registry.yarnpkg.com/ajv-keywords/-/ajv-keywords-3.1.0.tgz#ac2b27939c543e95d2c06e7f7f5c27be4aa543be"
|
||||
|
||||
ajv@^6.1.0:
|
||||
version "6.2.1"
|
||||
resolved "https://registry.yarnpkg.com/ajv/-/ajv-6.2.1.tgz#28a6abc493a2abe0fb4c8507acaedb43fa550671"
|
||||
dependencies:
|
||||
fast-deep-equal "^1.0.0"
|
||||
fast-json-stable-stringify "^2.0.0"
|
||||
json-schema-traverse "^0.3.0"
|
||||
|
||||
big.js@^3.1.3:
|
||||
version "3.2.0"
|
||||
resolved "https://registry.yarnpkg.com/big.js/-/big.js-3.2.0.tgz#a5fc298b81b9e0dca2e458824784b65c52ba588e"
|
||||
|
||||
emojis-list@^2.0.0:
|
||||
version "2.1.0"
|
||||
resolved "https://registry.yarnpkg.com/emojis-list/-/emojis-list-2.1.0.tgz#4daa4d9db00f9819880c79fa457ae5b09a1fd389"
|
||||
|
||||
fast-deep-equal@^1.0.0:
|
||||
version "1.1.0"
|
||||
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-1.1.0.tgz#c053477817c86b51daa853c81e059b733d023614"
|
||||
|
||||
fast-json-stable-stringify@^2.0.0:
|
||||
version "2.0.0"
|
||||
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz#d5142c0caee6b1189f87d3a76111064f86c8bbf2"
|
||||
|
||||
json-schema-traverse@^0.3.0:
|
||||
version "0.3.1"
|
||||
resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-0.3.1.tgz#349a6d44c53a51de89b40805c5d5e59b417d3340"
|
||||
|
||||
json5@^0.5.0:
|
||||
version "0.5.1"
|
||||
resolved "https://registry.yarnpkg.com/json5/-/json5-0.5.1.tgz#1eade7acc012034ad84e2396767ead9fa5495821"
|
||||
|
||||
loader-utils@^1.1.0:
|
||||
version "1.1.0"
|
||||
resolved "https://registry.yarnpkg.com/loader-utils/-/loader-utils-1.1.0.tgz#c98aef488bcceda2ffb5e2de646d6a754429f5cd"
|
||||
dependencies:
|
||||
big.js "^3.1.3"
|
||||
emojis-list "^2.0.0"
|
||||
json5 "^0.5.0"
|
||||
|
||||
schema-utils@^0.4.5:
|
||||
version "0.4.5"
|
||||
resolved "https://registry.yarnpkg.com/schema-utils/-/schema-utils-0.4.5.tgz#21836f0608aac17b78f9e3e24daff14a5ca13a3e"
|
||||
dependencies:
|
||||
ajv "^6.1.0"
|
||||
ajv-keywords "^3.1.0"
|
||||
Reference in New Issue
Block a user