From f9bae6f57db538a27010dd7af16a8b9e02c545c2 Mon Sep 17 00:00:00 2001 From: CNine Date: Sun, 28 Jul 2019 19:38:04 +0800 Subject: [PATCH] [tech] init --- .gitignore | 88 +++++++++++++++++++++++++++++++++++++++++++++ README.md | 41 +++++++++++++++++++++ index.js | 1 + package.json | 27 ++++++++++++++ src/index.js | 18 ++++++++++ src/lib/config.js | 7 ++++ src/lib/injector.js | 30 ++++++++++++++++ src/lib/loader.js | 3 ++ yarn.lock | 34 ++++++++++++++++++ 9 files changed, 249 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 index.js create mode 100644 package.json create mode 100644 src/index.js create mode 100644 src/lib/config.js create mode 100644 src/lib/injector.js create mode 100644 src/lib/loader.js create mode 100644 yarn.lock diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed0d3c8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,88 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env +.env.test + +# parcel-bundler cache (https://parceljs.org/) +.cache + +# next.js build output +.next + +# nuxt.js build output +.nuxt + +# vuepress build output +.vuepress/dist + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..75593ca --- /dev/null +++ b/README.md @@ -0,0 +1,41 @@ +# Vue Filename Injector + +Inject the file path of the `.vue` on `this.$options.__source`. + +## Install + +``` sh +yarn add @d2-projects/vue-filename-injector -D +``` + +or + +``` sh +npm install @d2-projects/vue-filename-injector -D +``` + +## Usage + +Only used for vue-cli3, `vue.config.js`: + +``` js +const VueFilenameInjector = require('@d2-projects/vue-filename-injector') + +module.exports = { + + chainWebpack: config => { + // ... + + // in here, only with chainWebpack + VueFilenameInjector(config, { + propName: '__source' // default + }) + + } +} +``` + +## Relevant + +https://github.com/neutrinojs/webpack-chain +https://vue-loader.vuejs.org/guide/custom-blocks.html diff --git a/index.js b/index.js new file mode 100644 index 0000000..d12c5e7 --- /dev/null +++ b/index.js @@ -0,0 +1 @@ +module.exports = require('./src/index.js') diff --git a/package.json b/package.json new file mode 100644 index 0000000..12ea21f --- /dev/null +++ b/package.json @@ -0,0 +1,27 @@ +{ + "name": "@d2-projects/vue-filename-injector", + "version": "1.0.0", + "description": "Inject the file path of .vue", + "main": "index.js", + "repository": { + "type": "git", + "url": "git+https://github.com/d2-projects/vue-filename-injector.git" + }, + "keywords": [ + "vue", + "file", + "filename", + "path", + "injector", + "loader" + ], + "author": "CNine ", + "license": "MIT", + "bugs": { + "url": "https://github.com/d2-projects/vue-filename-injector/issues" + }, + "homepage": "https://github.com/d2-projects/vue-filename-injector#readme", + "dependencies": { + "loader-utils": "^1.2.3" + } +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..5288518 --- /dev/null +++ b/src/index.js @@ -0,0 +1,18 @@ +const { blockName } = require('./lib/config.js') + +// for chainWebpack +module.exports = function(config, options) { + config.module + .rule('vue') + .use('vue-filename-injector') + .loader(require.resolve('./lib/injector.js')) + .options(options) + .after('vue-loader') + .end() + config.module + .rule('') + .resourceQuery(new RegExp(`blockType=${blockName}`)) + .use('vue-filename-injector-loader') + .loader(require.resolve('./lib/loader.js')) + .end() +} diff --git a/src/lib/config.js b/src/lib/config.js new file mode 100644 index 0000000..7910167 --- /dev/null +++ b/src/lib/config.js @@ -0,0 +1,7 @@ +const defaultPropName = '__source' +const blockName = 'vue-filename-injector' + +module.exports = { + defaultPropName, + blockName +} diff --git a/src/lib/injector.js b/src/lib/injector.js new file mode 100644 index 0000000..390dd9c --- /dev/null +++ b/src/lib/injector.js @@ -0,0 +1,30 @@ +const path = require('path') +const loaderUtils = require('loader-utils') + +const { blockName, defaultPropName } = require('./config.js') + +module.exports = function (content /*, map, meta */) { + const loaderContext = this + + const { + rootContext, + resourcePath + } = loaderContext + + const context = rootContext || process.cwd() + const options = loaderUtils.getOptions(loaderContext) || {} + const rawShortFilePath = path + .relative(context, resourcePath) + .replace(/^(\.\.[\/\\])+/, '') + + const propName = options.propName || defaultPropName + + content += ` +<${blockName}> +export default function (Component) { + Component.options.${propName} = ${JSON.stringify(rawShortFilePath.replace(/\\/g, '/'))} +} + +` + return content +} diff --git a/src/lib/loader.js b/src/lib/loader.js new file mode 100644 index 0000000..66fc79d --- /dev/null +++ b/src/lib/loader.js @@ -0,0 +1,3 @@ +module.exports = function(source, map) { + this.callback(null, source, map) +} diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..4c5035d --- /dev/null +++ b/yarn.lock @@ -0,0 +1,34 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +big.js@^5.2.2: + version "5.2.2" + resolved "https://registry.npm.taobao.org/big.js/download/big.js-5.2.2.tgz#65f0af382f578bcdc742bd9c281e9cb2d7768328" + integrity sha1-ZfCvOC9Xi83HQr2cKB6cstd2gyg= + +emojis-list@^2.0.0: + version "2.1.0" + resolved "https://registry.npm.taobao.org/emojis-list/download/emojis-list-2.1.0.tgz#4daa4d9db00f9819880c79fa457ae5b09a1fd389" + integrity sha1-TapNnbAPmBmIDHn6RXrlsJof04k= + +json5@^1.0.1: + version "1.0.1" + resolved "https://registry.npm.taobao.org/json5/download/json5-1.0.1.tgz#779fb0018604fa854eacbf6252180d83543e3dbe" + integrity sha1-d5+wAYYE+oVOrL9iUhgNg1Q+Pb4= + dependencies: + minimist "^1.2.0" + +loader-utils@^1.2.3: + version "1.2.3" + resolved "https://registry.npm.taobao.org/loader-utils/download/loader-utils-1.2.3.tgz#1ff5dc6911c9f0a062531a4c04b609406108c2c7" + integrity sha1-H/XcaRHJ8KBiUxpMBLYJQGEIwsc= + dependencies: + big.js "^5.2.2" + emojis-list "^2.0.0" + json5 "^1.0.1" + +minimist@^1.2.0: + version "1.2.0" + resolved "https://registry.npm.taobao.org/minimist/download/minimist-1.2.0.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fminimist%2Fdownload%2Fminimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284" + integrity sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=