Skip to content

Commit

Permalink
feat(vue-css-loader): change dist file name to be parsed by vue-loader
Browse files Browse the repository at this point in the history
  • Loading branch information
zoomchan-cxj committed Oct 11, 2022
1 parent 751e2e0 commit b5d94e4
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 68 deletions.
2 changes: 1 addition & 1 deletion docs/en-us/hippy-vue/style.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ At present, the basic `Universal`, `Type`, `ID`, `Class`, `Grouping` selectors h
How to use?

1. Update `@hippy/vue` and `@hippy/vue-loader` to `2.15.0+` version.
1. Update `@hippy/vue` and `@hippy/vue-css-loader` to `2.15.0+` version.
2. Vue2.0 set `Vue.config.scoped = true;`(Vue2.0 `scoped` and `Attribute` disabled by default,Vue3.0 enabled by default)


Expand Down
2 changes: 1 addition & 1 deletion docs/hippy-vue/style.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ HippyVue 提供了 `beforeLoadStyle` 的 Vue options 勾子函数,供开发者
如何开启?

1. 升级 `@hippy/vue``@hippy/vue-loader``2.15.0+` 版本
1. 升级 `@hippy/vue``@hippy/vue-css-loader``2.15.0+` 版本
2. Vue2.0 设置全局开关 `Vue.config.scoped = true;`(Vue2.0 默认 `scoped``Attribute` 选择器能力关闭,Vue3.0 默认开启无需设置开关)


Expand Down
2 changes: 1 addition & 1 deletion examples/hippy-vue-demo/scripts/hippy-webpack.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const manifest = require('../dist/android/vendor-manifest.json');

const platform = 'android';
let cssLoader = '@hippy/vue-css-loader';
const hippyVueCssLoaderPath = path.resolve(__dirname, '../../../packages/hippy-vue-css-loader/dist/index.js');
const hippyVueCssLoaderPath = path.resolve(__dirname, '../../../packages/hippy-vue-css-loader/dist/css-loader.js');
if (fs.existsSync(hippyVueCssLoaderPath)) {
console.warn(`* Using the @hippy/vue-css-loader in ${hippyVueCssLoaderPath}`);
cssLoader = hippyVueCssLoaderPath;
Expand Down
2 changes: 1 addition & 1 deletion examples/hippy-vue-demo/scripts/hippy-webpack.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const pkg = require('../package.json');

let cssLoader = '@hippy/vue-css-loader';
const hippyVueCssLoaderPath = path.resolve(__dirname, '../../../packages/hippy-vue-css-loader/dist/index.js');
const hippyVueCssLoaderPath = path.resolve(__dirname, '../../../packages/hippy-vue-css-loader/dist/css-loader.js');
if (fs.existsSync(hippyVueCssLoaderPath)) {
console.warn(`* Using the @hippy/vue-css-loader in ${hippyVueCssLoaderPath}`);
cssLoader = hippyVueCssLoaderPath;
Expand Down
2 changes: 1 addition & 1 deletion examples/hippy-vue-demo/scripts/hippy-webpack.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const manifest = require('../dist/ios/vendor-manifest.json');

const platform = 'ios';
let cssLoader = '@hippy/vue-css-loader';
const hippyVueCssLoaderPath = path.resolve(__dirname, '../../../packages/hippy-vue-css-loader/dist/index.js');
const hippyVueCssLoaderPath = path.resolve(__dirname, '../../../packages/hippy-vue-css-loader/dist/css-loader.js');
if (fs.existsSync(hippyVueCssLoaderPath)) {
console.warn(`* Using the @hippy/vue-css-loader in ${hippyVueCssLoaderPath}`);
cssLoader = hippyVueCssLoaderPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const pkg = require('../package.json');

let cssLoader = '@hippy/vue-css-loader';
const hippyVueCssLoaderPath = path.resolve(__dirname, '../../../packages/hippy-vue-css-loader/dist/index.js');
const hippyVueCssLoaderPath = path.resolve(__dirname, '../../../packages/hippy-vue-css-loader/dist/css-loader.js');
if (fs.existsSync(hippyVueCssLoaderPath)) {
console.warn(`* Using the @hippy/vue-css-loader in ${hippyVueCssLoaderPath}`);
cssLoader = hippyVueCssLoaderPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const pkg = require('../package.json');

let cssLoader = '@hippy/vue-css-loader';
const hippyVueCssLoaderPath = path.resolve(__dirname, '../../../packages/hippy-vue-css-loader/dist/index.js');
const hippyVueCssLoaderPath = path.resolve(__dirname, '../../../packages/hippy-vue-css-loader/dist/css-loader.js');
if (fs.existsSync(hippyVueCssLoaderPath)) {
console.warn(`* Using the @hippy/vue-css-loader in ${hippyVueCssLoaderPath}`);
cssLoader = hippyVueCssLoaderPath;
Expand Down
2 changes: 1 addition & 1 deletion packages/hippy-vue-css-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@hippy/vue-css-loader",
"version": "2.14.7",
"description": "hippy-vue style loader module for webpack",
"main": "dist/index.js",
"main": "dist/css-loader.js",
"homepage": "https://hippyjs.org",
"repository": "https://github.com/Tencent/Hippy",
"author": "OpenHippy Team",
Expand Down
75 changes: 75 additions & 0 deletions packages/hippy-vue-css-loader/src/css-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Tencent is pleased to support the open source community by making
* Hippy available.
*
* Copyright (C) 2017-2022 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import crypto from 'crypto';
import { getOptions } from 'loader-utils';
import { GLOBAL_STYLE_NAME, GLOBAL_DISPOSE_STYLE_NAME } from '@vue/runtime/constants';
import parseCSS from './css-parser';
import translateColor from './color-parser';

let sourceId = 0;

/**
* Convert the CSS text to AST that able to parse by selector parser.
*/
function hippyVueCSSLoader(source) {
const options = getOptions(this);
const parsed = parseCSS(source, { source: sourceId });

const majorNodeVersion = parseInt(process.versions.node.split('.')[0], 10);
const hashType = majorNodeVersion >= 17 ? 'md5' : 'md4';
const hash = crypto.createHash(hashType);
const contentHash = hash.update(source).digest('hex');
sourceId += 1;
const rulesAst = parsed.stylesheet.rules.filter(n => n.type === 'rule').map(n => ({
hash: contentHash,
selectors: n.selectors,
declarations: n.declarations.map((dec) => {
let { value } = dec;
if (dec.property && dec.property.toLowerCase().indexOf('color') > -1) {
value = translateColor(value, options);
}
return {
type: dec.type,
property: dec.property,
value,
};
}),
}));
const code = `(function() {
if (!global['${GLOBAL_STYLE_NAME}']) {
global['${GLOBAL_STYLE_NAME}'] = [];
}
global['${GLOBAL_STYLE_NAME}'] = global['${GLOBAL_STYLE_NAME}'].concat(${JSON.stringify(rulesAst)});
if(module.hot) {
module.hot.dispose(() => {
console.log('disposeHandlers');
if(!global['${GLOBAL_DISPOSE_STYLE_NAME}']) {
global['${GLOBAL_DISPOSE_STYLE_NAME}'] = [];
}
global['${GLOBAL_DISPOSE_STYLE_NAME}'] = global['${GLOBAL_DISPOSE_STYLE_NAME}'].concat('${contentHash}');
})
}
})()`;
return `module.exports=${code}`;
}

export default hippyVueCSSLoader;
60 changes: 3 additions & 57 deletions packages/hippy-vue-css-loader/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Tencent is pleased to support the open source community by making
* Hippy available.
*
* Copyright (C) 2017-2019 THL A29 Limited, a Tencent company.
* Copyright (C) 2017-2022 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -17,59 +17,5 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import crypto from 'crypto';
import { getOptions } from 'loader-utils';
import { GLOBAL_STYLE_NAME, GLOBAL_DISPOSE_STYLE_NAME } from '@vue/runtime/constants';
import parseCSS from './css-parser';
import translateColor from './color-parser';

let sourceId = 0;

/**
* Convert the CSS text to AST that able to parse by selector parser.
*/
function hippyVueCSSLoader(source) {
const options = getOptions(this);
const parsed = parseCSS(source, { source: sourceId });

const majorNodeVersion = parseInt(process.versions.node.split('.')[0], 10);
const hashType = majorNodeVersion >= 17 ? 'md5' : 'md4';
const hash = crypto.createHash(hashType);
const contentHash = hash.update(source).digest('hex');
sourceId += 1;
const rulesAst = parsed.stylesheet.rules.filter(n => n.type === 'rule').map(n => ({
hash: contentHash,
selectors: n.selectors,
declarations: n.declarations.map((dec) => {
let { value } = dec;
if (dec.property && dec.property.toLowerCase().indexOf('color') > -1) {
value = translateColor(value, options);
}
return {
type: dec.type,
property: dec.property,
value,
};
}),
}));
const code = `(function() {
if (!global['${GLOBAL_STYLE_NAME}']) {
global['${GLOBAL_STYLE_NAME}'] = [];
}
global['${GLOBAL_STYLE_NAME}'] = global['${GLOBAL_STYLE_NAME}'].concat(${JSON.stringify(rulesAst)});
if(module.hot) {
module.hot.dispose(() => {
console.log('disposeHandlers');
if(!global['${GLOBAL_DISPOSE_STYLE_NAME}']) {
global['${GLOBAL_DISPOSE_STYLE_NAME}'] = [];
}
global['${GLOBAL_DISPOSE_STYLE_NAME}'] = global['${GLOBAL_DISPOSE_STYLE_NAME}'].concat('${contentHash}');
})
}
})()`;
return `module.exports=${code}`;
}

export default hippyVueCSSLoader;
import defaultExport from './css-loader';
export default defaultExport;
2 changes: 1 addition & 1 deletion packages/hippy-vue-loader/lib/loaders/pitcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const stylePostLoaderPath = require.resolve('./stylePostLoader');

const isESLintLoader = l => /(\/|\\|@)eslint-loader/.test(l.path);
const isNullLoader = l => /(\/|\\|@)null-loader/.test(l.path);
const isCSSLoader = l => /(\/|\\|@?)css-loader/.test(l.path);
const isCSSLoader = l => /(\/|\\|@)css-loader/.test(l.path);
const isCacheLoader = l => /(\/|\\|@)cache-loader/.test(l.path);
const isPitcher = l => l.path !== __filename;
const isPreLoader = l => !l.pitchExecuted;
Expand Down
10 changes: 8 additions & 2 deletions scripts/vue-configs.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,12 @@ const builds = {
banner: banner('@hippy/vue', hippyVuePackage.version),
},
'@hippy/vue-css-loader': {
entry: resolvePackage('hippy-vue-css-loader', 'src/index.js'),
dest: resolvePackage('hippy-vue-css-loader', 'dist/index.js'),
entry: {
index: resolvePackage('hippy-vue-css-loader', 'src/index.js'),
'css-loader': resolvePackage('hippy-vue-css-loader', 'src/css-loader.js'),
},
dir: resolvePackage('hippy-vue-css-loader', 'dist'),
entryFileNames: '[name].js',
format: 'cjs',
moduleName: 'hippy-vue-css-loader',
banner: banner('@hippy/vue-css-loader', cssLoaderPackage.version),
Expand Down Expand Up @@ -175,6 +179,8 @@ function genConfig(name) {
}),
].concat(opts.plugins || []),
output: {
entryFileNames: opts.entryFileNames,
dir: opts.dir,
file: opts.dest,
format: opts.format,
banner: opts.banner,
Expand Down

0 comments on commit b5d94e4

Please sign in to comment.